package com.dimong.loan.sdk.app;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import com.dimong.loan.sdk.utils.Utils;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;

import java.util.Map;

public abstract class BaseApplication extends Application {

    public final String TAG = getClass().getSimpleName();

    private boolean isBackground = false;
    private int activityCount;
    private static BaseApplication instance;

    /**
     * 初始化全局变量参数
     */
    public abstract void initAppParam();

    /**
     * 初始化第三方SDK
     */
    public abstract void initThirdLibrary();

    /**
     * == onLowMemory()
     */
    public abstract void onAppLowMemory();

    /**
     * == onTerminate()
     */
    public abstract void onAppTerminate();

    /**
     * == initDBHelper()
     * 初始化数据库
     */
    public abstract void initDBHelper();

    /**
     * == initDirectory()
     * 初始化文件目录
     */
    public abstract void initDirectory();


    /**
     * == initPushError()
     * 全局错误上报
     */
    public abstract void initPushError();

    /**
     * == initNet()
     * 初始化网络API
     */
    public abstract void initNet();

    /**
     * == setDebug()
     * 初始化是否开启debug
     */
    public abstract void setDebug();

    public abstract void initImageLoader();

    /**
     * == onAppBackground()
     * 前台进入后台
     */
    public abstract void onAppBackground();

    /**
     * == onAppForeground()
     * 后台进入前台
     */
    public abstract void onAppForeground(Activity activity);

    protected boolean isDebug;//BuildConfig对于SDK一直是false

    public abstract boolean isCurProcess();

    /**
     * == onAllProgressInit()
     * 所有进程都要初始化
     */
    public abstract void onAllProgressInit();

    /**
     * == 初始化用户数据
     * 后台进入前台
     */
    public abstract void initUserInfoFromDB();


    public abstract void initSharedPreferences();

    public abstract Map<String, String> getHttpHeaders();

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        if (isCurProcess()) {
            instance = this;
            setDebug();
            initAppParam();
            initLogHelper();
            initDBHelper();
            initSharedPreferences();
            initUserInfoFromDB();
            initDirectory();
            initImageLoader();
            initPushError();
            initNet();
            initThirdLibrary();
            registerActivityLifecycleCallbacks(mActivityLifecycleCallbacks);

            UMConfigure.init(this, UMConfigure.DEVICE_TYPE_PHONE, "");
            MobclickAgent.setCatchUncaughtExceptions(false);
            UMConfigure.setLogEnabled(false);
        }
        onAllProgressInit();
    }

    @Override
    public void onLowMemory() {
        // TODO Auto-generated method stub
        super.onLowMemory();
        onAppLowMemory();
    }

    @Override
    public void onTerminate() {
        // TODO Auto-generated method stub
        super.onTerminate();
        onAppTerminate();
    }

    /**
     * @return void 返回类型
     * @throws
     * @Title: initLogHelper
     * @Description: 初始化日志
     * @author harry
     * @date 2013-5-27 下午5:42:14
     */
    public void initLogHelper() {
        Logger.addLogAdapter(new AndroidLogAdapter() {
            @Override public boolean isLoggable(int priority, String tag) {
                return isDebug();
            }
        });
    }

    public boolean isDebug() {
        return isDebug;
    }

    private final ActivityLifecycleCallbacks mActivityLifecycleCallbacks = new ActivityLifecycleCallbacks() {
        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
            Logger.e("onActivityCreated", activity.getClass().getName());
        }

        @Override
        public void onActivityStarted(Activity activity) {
            Logger.e("onActivityCreated", activity.getClass().getName());
        }

        @Override
        public void onActivityResumed(Activity activity) {
            activityCount++;
            if (isBackground) {
                isBackground = false;
                onAppForeground(activity);
            }
        }

        @Override
        public void onActivityPaused(Activity activity) {

        }

        @Override
        public void onActivityStopped(Activity activity) {
            activityCount--;
            if (activityCount == 0 || Utils.isAppBackgroundRunning(BaseApplication.this)) {
                if (!isBackground) {
                    isBackground = true;
                    onAppBackground();
                }
            }


        }

        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

        }

        @Override
        public void onActivityDestroyed(Activity activity) {

        }
    };

    public boolean isBackground() {
        return isBackground;
    }

    public static BaseApplication getApplication() {
        return instance;
    }


}
